package com.babble.comment.controller;

import com.babble.comment.dto.CommentDTO;
import com.babble.comment.dto.QueryCommentDTO;
import com.babble.comment.entity.Comment;
import com.babble.comment.service.ICommentService;
import com.babble.comment.vo.CommentVO;
import com.babble.comment.vo.UserCommentsVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

@Validated
@RestController
@RequestMapping("comment")
@AllArgsConstructor
@Api(value = "评论模块", tags = "评论接口")
public class CommentController {

	private ICommentService commentService;

	@GetMapping("list/{blogId}")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "获取评论", notes = "获取所有一级评论包括子评论")
	public R<Page<CommentVO>> list(@PathVariable(value = "blogId", required = false) String blogId,
								   @ApiParam(value = "评论来源", example = "1：博客信息、2：问答、3：留言板、4：关于", defaultValue = "1") @RequestParam(value = "source", defaultValue = "1") int source,
								   Query query) {
		return R.data(commentService.list(blogId, source, query));
	}

	@PostMapping("create")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "添加评论", notes = "用户添加评论")
	public R<Comment> create(@RequestBody @Validated CommentDTO commentDTO) {
		return R.data(commentService.create(commentDTO));
	}

	@DeleteMapping("delete")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "删除评论", notes = "删除单个评论")
	public R delete(@RequestParam @NotEmpty(message = "不能为空") String id, @RequestParam @NotEmpty(message = "不能为空") String blogId) {
		commentService.delete(id, blogId);
		return R.success("删除成功！");
	}

	@GetMapping("user")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "评论回复", notes = "获取当前用户发表的评论/回复")
	public R<UserCommentsVO> getUserComments() {
		return R.data(commentService.getUserComments());
	}

	@GetMapping("query-page")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取所有评论", notes = "根据查询条件分页获取所有评论")
	public R<Page<CommentVO>> queryPage(QueryCommentDTO queryCommentDTO) {
		return R.data(commentService.queryPage(queryCommentDTO));
	}
}
